import api from '../api'
import tipBox from "../plugins";
export default {
    loadSearchData({ commit }) {
        api.loadWords({
            platform: "H5",
            code: "hotWord,sketch"
        }).then(({ success, data: { hotWord, sketch } }) => {
            if (!success) {
                return Promise.reject("请求失败");
            }
            commit("sethotWord", hotWord.data);
            commit("setsketch", sketch.data.query);
        }).catch((error) => {
            // console.log("请求失败", error);
            tipBox.showMsg("数据加载失败","error",2000);
        })
    },
    loadCommentData({ commit }, params) {
        api.loadComments({
            type: 1,
            itemId: params.iid,
            appPlat: "m"
        }).then(({ success, data }) => {
            if (!success) {
                return Promise.reject("请求失败");
            }
            commit("setcomments", data || null);
        }).catch((error) => {
            // console.log("请求失败", error);
            tipBox.showMsg("数据加载失败","error",2000);
        })
    },
    loadCartList({ commit }){
        // 真实项目，应该通过对应请求获取构成车数据
        let cartStr = sessionStorage.getItem("carts")||"{}";
        commit("setcartList",JSON.parse(cartStr));
    },
    addProductToCartList({ commit,state }, product) {
        // 真实项目，应该通过对应请求获取构成车数据
        let iid = product.iid;
        let carts = state.cartList; 
        if(carts[iid]){
            carts[iid].count += 1;
        }else{
            product.count = 1;
            carts[iid] = product;
        }
        sessionStorage.setItem("carts",JSON.stringify(carts));
        commit("setcartList",carts);        
    }
}